package com.yxh.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.yxh.base.*;
import com.yxh.common.annotation.Authorization;
import com.yxh.pojo.Menu;
import com.yxh.pojo.User;
import com.yxh.service.MenuService;
import com.yxh.service.UserService;
import com.yxh.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import static com.yxh.util.APIErrorCode.SUCCESS;

/**
 * Created by 32988 on 2018/6/1.
 */
@Controller
@RequestMapping("/menu/")
public class MenuController extends com.yxh.base.BaseController {

    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);

    private Response response;

    @Autowired
    private MenuService menuService;

    /**
     * 新增
     *
     * @param menu
     * @return
     */
    @PostMapping("save_menu")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response saveMenu(@RequestBody Menu menu) {
        menuService.save(menu);
        return Response.returnResponse(SUCCESS);
    }

    /**
     * 查询
     *
     * @param menu
     * @return
     */
    @GetMapping("get_information")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response getInformation(Menu menu, String page,String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = menuService.getInformation(menu,limit,offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);

        return response;
    }

    /**
     * 修改
     *
     * @param menu
     * @return
     */
    @PutMapping("update_menuInfo")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response updateMenuInfo(@RequestBody Menu menu) {
        menuService.update(menu);
        response = Response.returnResponse(SUCCESS);
        return response;
    }

    /**
     * 删除
     *
     * @return
     */
    @DeleteMapping("/{id}")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response deleteMenu(@PathVariable("id") String id) {
        menuService.delete(id);
        response = Response.returnResponse(SUCCESS);
        return response;
    }
}
